/*
 * @Author: zhengwenbin
 * @Date:   2017-05-04 17:51:29
 */

/** ================================================ 服务承诺组件 star=======================================================  */
Comps.PromiseComp = function(option) {
    this.compId = option.compId;
    this.needAsyncInit = option.needAsyncInit;
    this.promiseName = option.promiseName;
    this.imgSrc = option.imgSrc;
    this.defineUrl = option.defineUrl;
}

Comps.PromiseComp.prototype.init = function(){
    var that = this;
    $("#" + that.compId + "_a").attr("href",that.defineUrl);
    $("#" + that.compId + "_name").html(that.promiseName);
    Utils.ajaxLoadComplate(that);
}
/** ================================================ 服务承诺组件 end=======================================================  */


/** ================================================ 订单查询组件 star=======================================================  */
Comps.OrderSearchComp = function(option) {
    this.compId = option.compId;
    this.needAsyncInit = option.needAsyncInit;
    this.codeUrl = option.codeUrl;
    this.orderSearchUrl = option.orderSearchUrl;
    this.searchTitle = option.searchTitle;
}
Comps.OrderSearchComp.prototype.init = function(){
    var that = this;
    Utils.ajaxLoadComplate(that);
    var needAsyncInit = that.needAsyncInit;
    if (needAsyncInit) {
        $("#" + that.compId + "_btn").html(that.searchTitle);
    }
    that.bindSomeEvent();
}
Comps.OrderSearchComp.prototype.bindSomeEvent=function(){
    var that=this;
    $("#"+that.compId+"_a").click(function(){
        var phone = $("#"+that.compId+"_phone").val();
        var reg = /^1\d{10}$/;
		if(phone==null || phone ==""){
			alert("请输入手机号");
			return;
		}else if(!reg.test(phone)){
			alert("请输入正确的手机号");
			return;
		}
        //$("#" + that.compId + "_a").attr("href",that.codeUrl+"?phone="+phone);
        //window.open(ContextPath+that.codeUrl+"?phone="+phone);
		var url = ContextPath+that.codeUrl;	
		$.getJSON(url,{phone:phone, ranNum:Math.random()},function(data){
			if(data.flag=="sendRegMsgFail"){
				alert("短信验证码发送失败!");
				return;
			}else{
				var sessionId = data.sessionId;
				$("#"+that.compId).append("<input type='hidden' id='sessionId'/>");
				$("#sessionId").val(sessionId);
				timeout(that);
			}
		})
    });
    $("#"+that.compId+"_btn").click(function(){
        var phone = $("#"+that.compId+"_phone").val();
        var code = $("#"+that.compId+"_code").val();
        var sessionId = $("#sessionId").val();
        if(sessionId == null ||sessionId =="undefine"){
        	sessionId = "";
        }
        var reg = /^1\d{10}$/;
        if(phone==null || phone ==""){
			alert("请输入手机号");
			return;
		}else if(!reg.test(phone)){
			alert("请输入正确的手机号");
			return;
		}else if(code==null || code ==""){
			alert("验证码不能为空");
			return;
		}
        window.open(ContextPath+that.orderSearchUrl+"?phone="+phone+"&code="+code+"&sessionId="+sessionId);
    })

}
var wait=60;
function timeout(o) {
  if (wait == 0) {
	  //获取验证码按钮解除禁用
	  $("#"+o.compId+"_a").attr("disabled", false);
	  $("#"+o.compId+"_a").text("重新获取验证码");
      wait = 60;
  } else {
     // 禁用获取验证码按钮
	  $("#"+o.compId+"_a").attr("disabled", true);
      $("#"+o.compId+"_a").text(wait+"秒后重发");
      wait--;
      setTimeout(function() {
    	  timeout(o);
      },
      1000)
  }
}
/** ================================================ 订单查询组件 end=======================================================  */


/** ================================================ 频道组件 star=======================================================  */
Comps.ChannelComp = function (config) {
    this.compId = config.compId;
    this.config = config;
};
Comps.ChannelComp.prototype.init = function () {
    var that = this;
    var compId = that.compId;
    var needAsyncInit = that.config.needAsyncInit;
    if (needAsyncInit) {
        that.ajaxLoadData();
    }else {
        Utils.ajaxLoadComplate(that);
    }

    that.setStyles(compId);

};

Comps.ChannelComp.prototype.setStyles = function (compId) {
    //导航下拉
    var time = null;
    var list = $("#"+compId+"_navlist");
    var box = $("#"+compId+"_navbox");
    var lista = list.find(".m-drop");

    for(var i=0,j=lista.length;i<j;i++){
        if(lista[i].className == "now"){
            var olda = i;
        }
    }

    var box_show = function(hei){
        box.stop().animate({
            height:hei,
            opacity:1
        },400);
    }

    var box_hide = function(){
        box.stop().animate({
            height:0,
            opacity:0
        },400);
    }

    lista.hover(function(){
        lista.removeClass("now");
        $(this).addClass("now");
        clearTimeout(time);
        var index = list.find(".m-drop").index($(this));
        box.find(".m-navdropbox").hide().eq(index).show();
        var _height = box.find(".m-navdropbox").eq(index).height()+20;
        box_show(_height)
    },function(){
        time = setTimeout(function(){
            box.find(".m-navdropbox").hide();
            box_hide();
        },50);
        lista.removeClass("now");
        lista.eq(olda).addClass("now");
    });

    box.find(".m-navdropbox").hover(function(){
        var _index = box.find(".m-navdropbox").index($(this));
        lista.removeClass("now");
        lista.eq(_index).addClass("now");
        clearTimeout(time);
        $(this).show();
        var _height = $(this).height()+20;
        box_show(_height);
    },function(){
        time = setTimeout(function(){
            $(this).hide();
            box_hide();
        },50);
        lista.removeClass("now");
        lista.eq(olda).addClass("now");
    });
}

Comps.ChannelComp.prototype.ajaxLoadData = function () {
    var ts = this;
    var compId = this.config.compId;
    var channels = this.config.channels;
    $.ajax({
        "url": ContextPath+"/channelComp/ajaxLoadData.json",
        data: {"channels": channels},
        "success": function (data) {
            ts.render(data.data);
            Utils.ajaxLoadComplate(ts);
        }
    })
}
Comps.ChannelComp.prototype.render = function (data) {
    var that = this;
    var compId = that.compId;

    var firChannelEle =$("#" + compId + "_navlist");
    var secChannelEle =$("#" + compId + "_secChannel");

    var firChannelModelEle = firChannelEle.find(".j_loopLi").first();

    var length = data.length;
    for (var i = 0; i < length; ++i) {
        var channelDTO = data[i];
        //克隆<li>标签
        var newEle = firChannelModelEle.clone();

        //分类渲染
        var channelUuid = channelDTO.uuid;
        var channelUrl;
        if (!channelDTO.channelUrl) {
            channelUrl = that.customUrl + channelDTO.pageUuid;
        } else {
            channelUrl = channelDTO.channelUrl;
        }
        //获取循环的a标签
        var aEle = newEle.find(".j_loopA").first();

        aEle.html(channelDTO.channelName);
        aEle.attr("href", ContextPath + channelUrl);
        //给a赋id属性，以便后续取子频道数据
        aEle.attr("id", channelUuid);
        var recommendChannels = that.recommendChannels;
        if(recommendChannels){
            var channelArr = recommendChannels.split(";");
            for(var j = 0;j< channelArr.length ;j++){
                var recommendChannel = channelArr[j];
                if(recommendChannel){
                    var uuid = recommendChannel.split("_")[0];
                    if(channelUuid == uuid){
                        aEle.append("<i></i>");
                    }
                }
            }
        }
        //获取子频道
        var secChannelDTOS = channelDTO.childChannelList;
        //有二级频道则遍历子频道，将二级频道渲染进页面
        if (secChannelDTOS) {
            newEle.attr("class", "m-drop");

            if(secChannelEle) {
                var secChannelModelEle = $("#"+compId + "_secChannelMoudle");
                //一个一级频道克隆一个二级频道框
                var newSecEle = secChannelModelEle.clone();
                newSecEle.attr("name", channelUuid + "_secChannel");

                for (var j = 0;j<secChannelDTOS.length;j++) {
                    var secChannel = secChannelDTOS[j];
                    var secChannelUrl;
                    if (!secChannel.channelUrl) {
                        secChannelUrl = that.customUrl + secChannel.pageUuid;
                    } else {
                        secChannelUrl = secChannel.channelUrl;
                    }

                    //一个二级频道框里面包含多个二级频道
                    var divSecEle = newSecEle.find(".j_secChannelDiv").first().clone();

                    var aSecEle = divSecEle.find(".j_secChannelA").first();
                    //给a标签赋地址
                    if(!"积分换购" == secChannel.channelName){
                    aSecEle.attr("href", ContextPath + secChannelUrl);

                    }
                    if("积分换购" == secChannel.channelName){
                    divSecEle.append("<div class='y_disabled'><span>敬请期待</span></div>");
                    }
                    //二级频道图片
                    var imgEle = aSecEle.find(".j_secChannelImg").first();
                    imgEle.attr("src", secChannel.imgUrl);
                    //二级频道内容
                    var pEle = aSecEle.find(".j_secChannelName").first();
                    pEle.html(secChannel.channelName);

                    newSecEle.append(divSecEle);
                }
                secChannelEle.append(newSecEle);
                newSecEle.find(".j_secChannelDiv").first().remove();
            }
        }
        firChannelEle.append(newEle);
    }
    if(firChannelModelEle) {
        //删除循环模板
        firChannelModelEle.remove();
    }
    if(secChannelEle) {
        var secChannelModelEle = $("#"+compId + "_secChannelMoudle");
        if(secChannelModelEle) {
            secChannelModelEle.remove();
        }
    }
}
/** ================================================ 频道组件 end=======================================================  */

/** ================================================ 品牌简介组件 star=======================================================  */
Comps.BrandProfileComp = function (config) {
    this.compId = config.compId;
    this.config = config;
};
Comps.BrandProfileComp.prototype.init = function () {
    var that = this;
    Utils.ajaxLoadComplate(that);

};
/** ================================================ 品牌简介组件 star=======================================================  */

/** ================================================ 品牌实力简介组件 star=======================================================  */
Comps.BrandStrengthComp = function (config) {
    this.compId = config.compId;
    this.config = config;
};
Comps.BrandStrengthComp.prototype.init = function () {
    var that = this;
    Utils.ajaxLoadComplate(that);

};
/** ================================================ 品牌实力简介组件 star=======================================================  */


/** ================================================ 技术专区组件 star=======================================================  */
Comps.TechZoneComp = function (config) {
    this.compId = config.compId;
    this.config = config;
};
Comps.TechZoneComp.prototype.init = function () {
    var that = this;
    Utils.ajaxLoadComplate(that);

};
/** ================================================ 技术专区组件 star=======================================================  */

